import torch
import torchvision
from PIL import Image
from torch import nn

image_path = "./image/airplane.png"
image = Image.open(image_path)
# print(image)PIL格式
image = image.convert('RGB')#png图片是4通道，除了RGB三通道之外还有一个透明度通道，所以使用image.convert('RGB')保留其颜色通道
transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32,32)),
                                            torchvision.transforms.ToTensor()])
image = transform(image)
print(image.shape)


class MyModule(nn.Module):
    def __init__(self):
        super(MyModule, self).__init__()
        self.model = nn.Sequential(
            nn.Conv2d(3,32,5,1,padding=2),
            nn.MaxPool2d(2),
            nn.Conv2d(32,32,5,1,2),
            nn.MaxPool2d(2),
            nn.Conv2d(32,64,5,1,2),
            nn.MaxPool2d(2),
            nn.Flatten(),
            nn.Linear(64*4*4,64),
            nn.Linear(64,10)
        )

    def forward(self,x):
        x = self.model(x)
        return x


model = torch.load("mymodule_29_gpu.pth",map_location=torch.device("cpu"))
# model = torch.load("mymodule_7.pth")
print(model)
image = torch.reshape(image,(1,3,32,32))
model.eval()
with torch.no_grad():#该步可以节约内存和性能
    output = model(image)
print(output)
print(output.argmax(1))